package at.codebase.db.entities;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
@Entity
public class EMessage implements IMessage {

	@Id
	private Long messageId;
	
	@Index
	@Load
	private Ref<EUser> addressor;
	@Index
	@Load
	private Ref<EUser> recipient;
	
	private boolean pm; 		//true of its a normal PM
	@Load
	private Ref<EItem> item;	//Null of its a normal PM
	
	private String text;
	
	private long timeSend;	
	private boolean unread;
	
	/*
	 * Objectify
	 */
	@SuppressWarnings("boxing")
	public static Key<EMessage> key(Long messageId) {
		return Key.create(EMessage.class, messageId);
	}
	
	public static Ref<EMessage> ref(Long messageId) {
		Key<EMessage> key = key(messageId);
		return Ref.create(key);
	}
	
	/*
	 * Constructors
	 */
	public EMessage() {

	}
	
	public EMessage(Ref<EUser> addressor, Ref<EUser> recipient, Ref<EItem> item, boolean pm, String text, long timeSend, boolean unread) {
		this.addressor = addressor;
		this.recipient = recipient;
		this.item = item;
		this.pm = pm;
		this.text = text;
		this.timeSend = timeSend;
		this.unread = unread;
	}
	
	public static EMessage createItemMessage(Ref<EUser> addressor, Ref<EUser> recipient, Ref<EItem> item, String text){
		return new EMessage(addressor, recipient, item, false, text, System.currentTimeMillis(), true);
	}
	
	public static EMessage createPrivateMessage(Ref<EUser> addressor, Ref<EUser> recipient, String text){
		return new EMessage(addressor, recipient, null, true, text, System.currentTimeMillis(), true);
	}
	
	

	public Long getId() {
		return messageId;
	}

	public void setId(Long messageId) {
		this.messageId = messageId;
	}

	public Long getMessageId() {
		return messageId;
	}

	public void setMessageId(Long messageId) {
		this.messageId = messageId;
	}

	public Ref<EUser> getAddressor() {
		return addressor;
	}

	public void setAddressor(Ref<EUser> addressor) {
		this.addressor = addressor;
	}

	public Ref<EUser> getRecipient() {
		return recipient;
	}

	public void setRecipient(Ref<EUser> recipient) {
		this.recipient = recipient;
	}

	public Ref<EItem> getItem() {
		return item;
	}

	public void setItem(Ref<EItem> item) {
		this.item = item;
	}
	
	public boolean isPm() {
		return pm;
	}

	public void setPm(boolean pm) {
		this.pm = pm;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public long getTimeSend() {
		return timeSend;
	}

	public void setTimeSend(long timeSend) {
		this.timeSend = timeSend;
	}

	public boolean isUnread() {
		return unread;
	}

	public void setUnread(boolean unread) {
		this.unread = unread;
	}

	@Override
	public String toString() {
		return "EMessage [id=" + messageId + ", addressor=" + addressor
				+ ", recipient=" + recipient + ", pm=" + pm + ", item=" + item
				+ ", text=" + text + ", timeSend=" + timeSend + ", unread="
				+ unread + "]";
	}
		
}
